//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.sys;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicAdmAreaCodeDao;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.SysMessageDao;
import com.aidong.model.LoginUser;
import com.aidong.model.wills.DicAdmAreaCode;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.SysMessage;
import com.aidong.model.wills.Upload;
import com.aidong.service.UploadService;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"sysMessage"})
public class SysMessageController extends BaseController {
    @Resource
    SysMessageDao sysMessageDao;
    @Resource
    UploadService uploadService;
    @Resource
    DicShopCodeDao dicShopCodeDao;
    @Resource
    DicAdmAreaCodeDao dicAdmAreaCodeDao;

    public SysMessageController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("sys/message/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "title",required = false) String title, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        if(!this.chkNull(status)) {
            cd.eq("status", status);
        } else {
            cd.gt("status", Integer.valueOf(0));
        }

        if(!this.chkNull(title)) {
            cd.like("title", "%" + title + "%");
        }

        this.sysMessageDao.queryPageList(pg, cd, (new OrderSet()).desc("msgId"));
        ModelAndView mv = new ModelAndView("sys/message/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"getShopList"})
    @ResponseBody
    public String getShopList() {
        StringBuilder sb = new StringBuilder();
        sb.append("{shops:[");
        List shops = this.dicShopCodeDao.getAllShopList();
        List areas = this.dicAdmAreaCodeDao.getAllAreaList();
        List areashops = this.dicAdmAreaCodeDao.getAllAreaShopList();
        int n = 0;

        DicShopCode i$;
        for(Iterator areatpMap = shops.iterator(); areatpMap.hasNext(); sb.append("[\"").append(i$.getCode()).append("\"").append(",\"").append(i$.getChName()).append("\"").append(",\"").append(this.nvl(i$.getChPinyin())).append("\"").append("]")) {
            i$ = (DicShopCode)areatpMap.next();
            if(n++ > 0) {
                sb.append(",");
            }
        }

        sb.append("],areas:[");
        n = 0;
        LinkedHashMap var12 = new LinkedHashMap();
        Iterator var13 = areas.iterator();

        while(var13.hasNext()) {
            DicAdmAreaCode t = (DicAdmAreaCode)var13.next();
            if(n++ > 0) {
                sb.append(",");
            }

            sb.append("[\"").append(t.getCode()).append("\"").append(",\"").append(t.getChName()).append("\"").append(",\"").append(t.getAreaType()).append("\"").append(",[");
            int ni = 0;
            Iterator i$1 = areashops.iterator();

            while(i$1.hasNext()) {
                String[] ss = (String[])i$1.next();
                if(t.getCode().equalsIgnoreCase(ss[0])) {
                    if(ni++ > 0) {
                        sb.append(",");
                    }

                    sb.append("\"").append(ss[1]).append("\"");
                }
            }

            sb.append("]]");
            if(!var12.containsKey(t.getAreaType())) {
                var12.put(t.getAreaType(), this.cache.getValue("admAreaType", t.getAreaType()));
            }
        }

        sb.append("],areatp:[");
        n = 0;

        String var14;
        for(var13 = var12.keySet().iterator(); var13.hasNext(); sb.append("[\"").append(var14).append("\"").append(",\"").append((String)var12.get(var14)).append("\"").append("]")) {
            var14 = (String)var13.next();
            if(n++ > 0) {
                sb.append(",");
            }
        }

        sb.append("]}");
        return this.getSuccess(sb.toString(), "");
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        SysMessage message = new SysMessage();
        message.setRecvCode("0-all");
        message.setStatus(Integer.valueOf(1));
        ModelAndView mv = new ModelAndView("sys/message/edit");
        mv.addObject("message", message);
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("_picPath", this.getSetting("pic.path"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("message") SysMessage message) {
        if(this.chkNull(message.getTitle())) {
            return this.getResult(false, "请输入标题");
        } else if(this.chkNull(message.getContent())) {
            return this.getResult(false, "请输入内容");
        } else if(!this.chkNull(message.getRecvCode()) && !this.chkNull(message.getRecvCode())) {
            if(this.chkNull(message.getStatus())) {
                message.setStatus(Integer.valueOf(1));
            }

            message.setSendTime(new Date());
            message.setCreateUser(this.getLoginUserCode());
            message.setCreateTime(new Date());
            message.setUpdateTime(new Date());

            try {
                this.sysMessageDao.add(message);
                this.addUserLog("新增系统通知", "编号:" + message.getMsgId() + ",标题:" + message.getTitle());
                this.addMsgNum(message.getRecvCode());
            } catch (Exception var3) {
                this.log.error("add sysmessage", var3);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "保存完成");
        } else {
            return this.getResult(false, "请选择接收门店");
        }
    }

    private void addMsgNum(String recvCode) {
        HashMap shopMap = new HashMap();
        boolean isall = false;
        String[] ss = recvCode.split(",");
        String[] list = ss;
        int i$ = ss.length;

        for(int user = 0; user < i$; ++user) {
            String s = list[user];
            if("0-all".equalsIgnoreCase(s)) {
                isall = true;
            } else if(s.startsWith("3-")) {
                shopMap.put(s.substring(2), s);
            }
        }

        List var9 = this.userSession.getLoginUserList();
        Iterator var10 = var9.iterator();

        while(true) {
            LoginUser var11;
            do {
                if(!var10.hasNext()) {
                    return;
                }

                var11 = (LoginUser)var10.next();
            } while(!isall && !shopMap.containsKey(var11.getShopCode()));

            var11.setMsgNum(var11.getMsgNum() + 1);
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("msgId") int msgId) {
        SysMessage message = (SysMessage)this.sysMessageDao.getById(Integer.valueOf(msgId));
        if(message == null) {
            return this.showAlert("没有这个通知");
        } else {
            ArrayList picList = null;
            if(!this.chkNull(message.getPicUrls())) {
                picList = new ArrayList();
                String[] mv = message.getPicUrls().split(",");
                String[] arr$ = mv;
                int len$ = mv.length;

                for(int i$ = 0; i$ < len$; ++i$) {
                    String s = arr$[i$];
                    picList.add(s);
                }
            }

            ModelAndView var9 = new ModelAndView("sys/message/edit");
            var9.addObject("message", message);
            var9.addObject("picList", picList);
            var9.addObject("_picPath", this.getSetting("pic.path"));
            var9.addObject("_oper", "edit");
            return var9;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("message") SysMessage message) {
        if(this.chkNull(message.getTitle())) {
            return this.getResult(false, "请输入标题");
        } else if(this.chkNull(message.getContent())) {
            return this.getResult(false, "请输入内容");
        } else if(!this.chkNull(message.getRecvCode()) && !this.chkNull(message.getRecvCode())) {
            if(this.chkNull(message.getStatus())) {
                message.setStatus(Integer.valueOf(1));
            }

            if(this.chkNull(message.getStatus())) {
                message.setStatus(Integer.valueOf(1));
            }

            SysMessage message1 = (SysMessage)this.sysMessageDao.getById(message.getMsgId());
            if(message1 == null) {
                return this.getResult(false, "没有这个通知");
            } else {
                message1.setTitle(message.getTitle());
                message1.setContent(message.getContent());
                message1.setRecvCode(message.getRecvCode());
                message1.setStatus(message.getStatus());
                message1.setUpdateTime(new Date());

                try {
                    this.sysMessageDao.update(message1);
                    this.addUserLog("修改系统通知", "编号:" + message.getMsgId() + ",标题:" + message.getTitle());
                } catch (Exception var4) {
                    this.log.error("edit sysmessage", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        } else {
            return this.getResult(false, "请选择接收门店");
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("msgId") int msgId) {
        SysMessage message = (SysMessage)this.sysMessageDao.getById(Integer.valueOf(msgId));
        if(message == null) {
            return this.getResult(false, "没有这个通知");
        } else {
            try {
                this.sysMessageDao.updateStatus(msgId, 0);
                this.addUserLog("删除系统通知", "编号:" + message.getMsgId() + ",标题:" + message.getTitle());
            } catch (Exception var4) {
                this.log.error("del sysmessage", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }

    @RequestMapping({"_upload"})
    @ResponseBody
    public String _upload(@RequestParam("file") MultipartFile file) {
        StringBuilder msg = new StringBuilder();
        Upload upload = this.uploadService.upload(file, msg);
        return upload == null?this.getResult(false, msg.toString()):this.getSuccess("{\"fid\":" + upload.getFileId() + ",\"furl\":\"" + upload.getUrl() + "\"}", "上传成功");
    }
}
